package com.wcl.demo.ui.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.easemob.chat.EMChatManager;
import com.easemob.exceptions.EaseMobException;
import com.wcl.demo.R;
import com.wcl.demo.db.MessageHelper;
import com.wcl.demo.model.entity.InviteMessage;

import java.util.List;

/**
 * Created by dllo on 15/11/25.
 */
public class InviteAdapter extends BaseAdapter {
    MessageHelper helper;

    private List<InviteMessage> datas;

    public InviteAdapter() {
        helper = new MessageHelper();
        this.datas = helper.queryMessage();
        notifyDataSetChanged();
    }

    public void addData() {
        this.datas = helper.queryMessage();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (datas != null && datas.size() > 0) {
            final ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.invite_item, null);
                holder.invite_contant = (TextView) convertView.findViewById(R.id.invite_contant);
                holder.invite_reason = (TextView) convertView.findViewById(R.id.invite_reason);
                holder.agreeBtn = (Button) convertView.findViewById(R.id.agreeBtn);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            final InviteMessage inviteMessage = datas.get(position);
            holder.invite_contant.setText(inviteMessage.getFrom());
            holder.invite_reason.setText(inviteMessage.getReason());
            if (inviteMessage.getStatus()==null||!(inviteMessage.getStatus() == 0)) {
                holder.agreeBtn.setText("已同意");

                InviteMessage message=helper.queryUser(inviteMessage.getFrom()).get(0);
                message.setFrom(inviteMessage.getFrom());
                message.setReason(inviteMessage.getReason());
                message.setStatus(1);
                helper.updateMessage(message);

                holder.agreeBtn.setEnabled(false);
            }
            Log.i("lnaou", inviteMessage.getFrom() + "===" + inviteMessage.getReason() + "<======="+inviteMessage.getStatus());
            holder.agreeBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        EMChatManager.getInstance().acceptInvitation(inviteMessage.getFrom());
                        Log.i("lanou","我已同意啦");
                    } catch (EaseMobException e) {
                        e.printStackTrace();
                    }
                    holder.agreeBtn.setText("已同意");

                    InviteMessage message=helper.queryUser(inviteMessage.getFrom()).get(0);
                    message.setFrom(inviteMessage.getFrom());
                    message.setReason(inviteMessage.getReason());
                    message.setStatus(1);
                    helper.updateMessage(message);

                  //  helper.updateStatus(inviteMessage.getFrom(),1);
                    holder.agreeBtn.setEnabled(false);

                }
            });
        }
        return convertView;
    }

    class ViewHolder {
        private TextView invite_contant, invite_reason;
        private Button agreeBtn;
    }
}
